/**
 * Created by 84071 on 2017-11-14.
 */
layui.define(['layer','laydate', 'form','element','upload'], function(exports){
    var $ = layui.jquery,
        layer = layui.layer,
        layelement = layui.element,
        layupload = layui.upload,
        laydate = layui.laydate,
        layform = layui.form;

    var see = {
        /*更改显示状态*/
        sestatus:function (o) {
            var d = $(o).data();
            $.ajax({
                async:false,
                type: "post",
                dataType: "json",
                data: {cxbsid:d.id},
                url: d.href,
                success:function (data) {
                    if(data.code == '1'){
                        layer.msg(data.msg,{icon:6});
                        location.reload();
                    }else{
                        layer.msg(data.msg,{icon:5});
                    }
                }
            })
        },
        /*单项删除*/
        deldata:function (o) {
            var d = $(o).data();
            layer.confirm("不可恢复！你确定要删除吗？", {icon: 3, title:"友情提示"}, function(index){
                $.ajax({
                    async:false,
                    type: "post",
                    dataType: "json",
                    data: {id:d.id},
                    url: d.href,
                    success:function (data) {
                        layer.msg(data.msg,{icon:7});
                        location.reload();
                    }
                })
                layer.close(index);
            });
        },
        //  选择图标
        upicon:function (o) {
            var d = $(o).data();
            layer.open({
                title: d.title,
                skin: 'cx-tclayui',
                type: 1,
                content: $(d.href),
                end: function(index, layero){
                    $(d.href).hide()
                }
            });
        },
        //  清空标签
        closdiv:function (o) {
            var d = $(o).data();
            $('.'+d.img).attr('src','');
            $('.'+d.val).val('');
            $(o).hide();
        },
        //  删除元素
        delclass:function(o){
            var d = $(o).data();
            $('.' + d.cid).remove();
        },
        // 清空表格
        clostr:function (o) {
            var trs = $(o).closest("tr");
            trs.find("input").remove();
            trs.find("img").hide();
            trs.hide();
        },
        // 关闭层
        close:function () {
            parent.layer.closeAll();
        },
        //  添加微信菜单
        wxmenu:function (o) {
            var d = $(o).data(),
                n = $(o).siblings('li').length;
            if(n < d.leng){
                var nm = $(o).prev().data('zmenu');
                if(nm > 0){
                    n = nm + 1;
                }
                $('.wx-menu').find('.open').removeClass('open');
                if(d.mc == 1){
                    var h = "<div class='layout wx-menuedit wxmenuedit"+n+"'><div class='layout cx-fex wx-menu-top'><h3>编辑菜单</h3><span class='button bg-red cx-click' data-type='wxdelmenu' data-cid='wxmenuadd"+n+"'>删除菜单</span></div><div class='cx-fex-l mb-15'><div class='x3 t-r'><label class='lab'>菜单名称</label></div><div class='xs8 xl9' data-cid='wxmenuadd"+n+"'><input type='text' name='button["+n+"][name]' class='ipt wx-ipt' placeholder='菜单名称' value=''></div></div><div class='cx-fex-l menuradio mb-15'><div class='x3 t-r'><label class='lab'>菜单内容</label></div><div class='xs8 xl9'><input class='menur"+n+"' name='button["+n+"][type]' type='radio' value='view' checked='checked' title='网页链接' lay-filter='wxmenuclass' data-num='button["+n+"]'><input class='menur"+n+"' name='button["+n+"][type]' type='radio' value='miniprogram' title='小程序' lay-filter='wxmenuclass' data-num='button["+n+"]'></div></div><div class='layout menuradiotext'><div class='cx-fex-l mb-15'><div class='x3 t-r'><label class='lab'>页面地址</label></div><div class='xs8 xl9'><input type='text' name='button["+n+"][url]' class='ipt' laceholder='' value=''></div></div></div></div>";
                    $(o).before("<li class='pos-r open wxmenuadd"+n+"' data-zmenu='"+n+"'><span class='wxmenuedit cx-click' data-type='wxeditmenu' data-cid='wxmenuedit"+n+"'>菜单名称</span><ul class='wx-menu-two cx-fex-c fex-column pos-a'><li class='layout cx-click wxmenuadd' data-type='wxmenu' data-zmenu='"+n+"' data-leng='5'>+ 添加子菜单</li></ul></li>");
                    $("#wxedit").append(h);
                    layform.render();
                    $(".wxmenuedit"+n).siblings().hide();
                }else{
                    var sn = $(o).parents('.wx-menu-two').parent().data('zmenu'),
                        h = "<div class='layout wx-menuedit wxmenuedit-two"+d.zmenu+n+"'><div class='layout cx-fex wx-menu-top'><h3>编辑子菜单</h3><span class='button bg-red cx-click' data-type='wxdelmenu' data-cid='wxmenuadd-two"+d.zmenu+n+"'>删除子菜单</span></div><div class='cx-fex-l mb-15'><div class='x3 t-r'><label class='lab'>子菜单名称</label></div><div class='xs8 xl9' data-cid='wxmenuadd-two"+d.zmenu+n+"'><input type='text' name='button["+sn+"][sub_button]["+n+"][name]' class='ipt wx-ipt' placeholder='子菜单名称' value=''></div></div><div class='cx-fex-l menuradio mb-15'><div class='x3 t-r'><label class='lab'>子菜单名称</label></div><div class='xs8 xl9'><input class='menur"+d.zmenu+n+"' name='button["+sn+"][sub_button]["+n+"][type]' type='radio' value='view' checked='checked' title='网页链接' lay-filter='wxmenuclass' data-num='button["+sn+"][sub_button]["+n+"]'><input class='menur"+d.zmenu+n+"' name='button["+sn+"][sub_button]["+n+"][type]' type='radio' value='miniprogram' title='小程序' lay-filter='wxmenuclass' data-num='button["+sn+"][sub_button]["+n+"]'></div></div><div class='layout menuradiotext'><div class='cx-fex-l mb-15'><div class='x3 t-r'><label class='lab'>页面地址</label></div><div class='xs8 xl9'><input type='text' name='button["+sn+"][sub_button]["+n+"][url]' class='ipt' placeholder='' value=''></div></div></div></div>";
                    $(o).before("<li class='pos-r open wxmenuadd-two"+d.zmenu+n+"' data-zmenu='"+n+"'><span class='wxmenuedit cx-click' data-type='wxeditmenu' data-zmenu='"+d.zmenu+"' data-cid='wxmenuedit-two"+d.zmenu+n+"'>子菜单名称</span></li>");
                    $("#wxedit").append(h);
                    layform.render();
                    $(".wxmenuedit-two"+ d.zmenu + n).siblings().hide();
                }
                var endnum = $(o).siblings('li').length;
                if(endnum >= d.leng){
                    $(o).remove();
                }
            }
        },
        //  编辑微信菜单
        wxeditmenu:function(o){
            var d = $(o).data();
            $(o).parents('.wx-menu').find('.open').removeClass('open');
            $(o).parent().addClass('open');
            $('.'+d.cid).show();
            $('.'+d.cid).siblings().hide();

        },
        //  删除微信菜单
        wxdelmenu:function (o) {
            var d = $(o).data(),
                add = $('.' + d.cid).parent().children('li.wxmenuadd').length,
                us = $('.' + d.cid).children('.wx-menu-two').length;

            if($('.' + d.cid).parent().hasClass('wx-menu-two')){
                var n = $('.' + d.cid).parent().children('li').length,
                    n = n - 1;
                if(n < 5){
                    if(add == 0){
                        $('.' + d.cid).parent().append("<li class='wxmenuadd cx-click' data-type='wxmenu' data-leng='5'>+ 添加子菜单</li>");
                    }
                }
            }else if($('.' + d.cid).parent().hasClass('wx-menu')){
                var n = $('.'+d.cid).parent().children('li').length,
                    n = n - 1;
                if(n < 3){
                    if(add == 0){
                        $('.' + d.cid).parent().append("<li class='wxmenuadd cx-click' data-type='wxmenu' data-leng='3' data-mc='1'>+ 添加菜单</li>");
                    }
                }
            }
            if(us == 1){
                $('.' + d.cid).children('.wx-menu-two').find('li.pos-r').each(function (index,item) {
                   var ld = $(item).find('span').data();
                    $('.'+ld.cid).remove();
                })
            }
            $('.'+d.cid).remove();
            $(o).parents('.wx-menuedit').remove();

        },
    };
    //  监听微信菜单选择
    layform.on('radio(wxmenuclass)', function(data){
        var n = $(data.elem).data('num');
        if(data.value == 'view'){
            var t = "<div class='cx-fex-l mb-15'><div class='x3 t-r'><label class='lab'>页面地址</label></div><div class='xs8 xl9'><input type='text' name='"+n+"[url]' class='ipt' laceholder='' value=''><span class='t-gray'>请输入带'http://'的链接地址</span></div></div>";
        }
        if(data.value == 'miniprogram'){
            var t = "<div class='cx-fex-l mb-15'><div class='x3 t-r'><label class='lab'>页面地址</label></div><div class='xs8 xl9'><input type='text' name='"+n+"[url]' class='ipt' laceholder='' value=''><span class='t-gray'>请输入带'http://'的链接地址</span><input type='text' name='"+n+"[appid]' class='ipt' laceholder='' value=''><span class='t-gray'>请输入小程序ID</span><input type='text' name='"+n+"[pagepath]' class='ipt' laceholder='' value=''><span class='t-gray'>请输入小程序页面地址</span></div></div>";
        }

        $(this).parents('.menuradio').siblings('.menuradiotext').html(t);
    });
    //  权限选择复选框
    layform.on('checkbox(authbox)', function(data){
        var dboxnum = $(this).closest("dl").find("dd input:checked").length;
        if($(this).parent().hasClass('dt')){
            $(this).closest("dl").find("input:checkbox").prop("checked",$(this).prop("checked"));
            if(data.elem.checked){
                $(this).closest("li.cx-fold-group").find("h4 input:checkbox").prop("checked",$(this).prop("checked"));
            }else{
                var tboxnum = $(this).closest(".cx-fold-cont").find("dt input:checked").length;
                if(tboxnum == '0'){
                    $(this).closest("li.cx-fold-group").find("h4 input:checkbox").prop("checked",$(this).prop("checked"));
                }
            }
        }else{
            if(data.elem.checked){
                $(this).closest("dl").find("dt input:checkbox").prop("checked",$(this).prop("checked"));
                $(this).closest("li.cx-fold-group").find("h4 input:checkbox").prop("checked",$(this).prop("checked"));
            }else{
                if(dboxnum == '0'){
                    $(this).closest("dl").find("dt input:checkbox").prop("checked",$(this).prop("checked"));
                }
                var tboxnum = $(this).closest(".cx-fold-cont").find("dt input:checked").length;
                if(tboxnum == '0'){
                    $(this).closest("li.cx-fold-group").find("h4 input:checkbox").prop("checked",$(this).prop("checked"));
                }
            }
        }
        layform.render();
    });
    // 删除与排序
    layform.on('submit(sd-btn)', function(data){
        var d = data.elem.dataset;
        if(d.type == 'sort'){
            $.ajax({
                type: "post",
                dataType: "json",
                data: data.field,
                url: d.href,
                success:function (data) {
                    layer.msg(data.msg,{icon:7});
                    location.reload();
                }
            })
        }
        if(d.type == 'edel'){
            layer.confirm("删除后，请至回收站找回", {icon: 3, title:"友情提示"}, function(index){
                $.ajax({
                    type: "post",
                    dataType: "json",
                    data: data.field,
                    url: d.href,
                    success:function (data) {
                        layer.msg(data.msg,{icon:7});
                        location.reload();
                    }
                })
                layer.close(index);
            })
        }
        if(d.type == 'pdel'){
            layer.confirm("批量后不可恢复！你确定要删除吗？", {icon: 3, title:"友情提示"}, function(index){
                $.ajax({
                    type: "post",
                    dataType: "json",
                    data: data.field,
                    url: d.href,
                    success:function (data) {
                        layer.msg(data.msg,{icon:7});
                        location.reload();
                    }
                })
                layer.close(index);
            })
        }
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });
    //  下拉菜单选择器
    layform.on('select(keys)', function(data){
        var old = $("input[name='keywords']").val();
        if(data.value != 0){
            if(old){
                $("input[name='keywords']").val(old + ',' + data.value)
            }else{
                $("input[name='keywords']").val(data.value)
            }
        }
    });
    layform.on('select(sources)', function(data){
        var href = $(data.elem).find("option:selected").text();
        if(data.value != 0) {
            $("input[name='sourceurl']").val(data.value);
            $("input[name='source']").val(href);
        }
    });
    //  监听提交按钮
    layform.on('submit(*)', function(data){
    });
    //  监听提交按钮
    layform.on('submit(order)', function(data){
        var s = $(data.elem).data('order'),
            h = data.form.action;
        if(s == '1'){
            data.field.ordersave = '1';
        }
        if(s == '2'){
            data.field.ordersave = '2';
        }
        $.ajax({
            type:'post',
            dataType: "json",
            data: data.field,
            url: h,
            success:function(data) {
                layer.open({
                    'title':false,
                    'content':data.msg
                })
            }
        });
        return false;
    });
    //  监听微信菜单提交
    layform.on('submit(wxmenusave)',function (data) {
        var s = $(data.elem).data('save'),
            h = data.form.action;
        data.field.wxsave = '0';
        if(s == '1'){
            data.field.wxsave = '1';
        }
        $.ajax({
            type:'post',
            dataType: "json",
            data: data.field,
            url: h,
            success:function(data) {
                layer.open({
                    'title':false,
                    'content':data.msg
                })
            }
        });
        return false;
    });
    //  系统地区选择
    layform.on('select(chinacode)', function(data){
        var o = data.elem,
            d = $(o).data(),
            nextleve = d.leve + 1;
        if(d.leve == 1){
            var x = '市';
        }
        if(d.leve == 2){
            var x = '县/区';
        }
        if(d.leve == 3){
            var x = "乡/镇/街道";
        }
        $.ajax({
            async:false,
            type: "post",
            dataType: "json",
            data: {zoneid:data.value,class:d.class},
            url: "/api/chinacode.html",
            success:function (res) {
                if(res.code == '1'){
                    var sele = res.dondata;
                    $(o).nextAll().remove();
                    $(o).after("<select class='cx-china-select' name='"+d.name+"["+ nextleve +"]' lay-filter='chinacode' data-leve='"+ nextleve +"' data-name='"+ d.name +"' data-class='don'></select>");
                    $(o).next().append("<option value=''>请选择"+ x +"</option>");
                    for(var i in sele){
                        $(o).next().append("<option value='"+sele[i].zoneid+"'>"+sele[i].zonename+"</option>");
                    }
                    layform.render('select');
                }
            }
        });
    });
    //同时绑定多个日期选择
    $(".cx-time").each(function(){
        laydate.render({
            elem: this,
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm:ss',
            calendar: true,
            min: '1900-1-1 00:00:00',
            trigger: 'click'
        });
    });

    $('body').on('click','.cx-click',function () {
        var a = $(this),
            b = a.data('type');
        see[b] ? see[b].call(this, a) : ""
    });
    exports('app', {});
});
